//
//  ThorBaseConfig.m
//  ThorWebView_Example
//
//  Created by panfei mao on 2019/1/14.
//  Copyright © 2019 panfei mao. All rights reserved.
//

#import "ThorBaseConfig.h"
#import <AFNetworking/AFNetworking.h>
#import <YTKNetwork/YTKNetwork.h>

#define ThorBaseUrl @"http://triskapi.ldxinyong.com"

@implementation ThorBaseConfig

+ (ThorBaseConfig *)sharedConfig{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _userPhone  = @"";
        _userId     = @"";
        _channel    = @"";
        // 配置网络请求
        [self configNetworkBaseParam];
    }
    return self;
}

- (void)loginWithUserPhone:(NSString *)userPhone
                 andUserId:(NSString *)userId
                andChannel:(NSString *)channel{
    if (userPhone && userPhone.length > 0) {
        [ThorBaseConfig sharedConfig].userPhone = userPhone;
    }
    if (userId && userId.length > 0) {
        [ThorBaseConfig sharedConfig].userId = userId;
    }
    if (channel && channel.length > 0) {
        [ThorBaseConfig sharedConfig].channel = channel;
    }
}

- (void)loginOut{
    [ThorBaseConfig sharedConfig].userPhone = @"";
    [ThorBaseConfig sharedConfig].userId = @"";
    [ThorBaseConfig sharedConfig].channel = @"";
}

//  配置请求基础参数
- (void)configNetworkBaseParam{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    config.baseUrl = ThorBaseUrl;
    
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    [securityPolicy setValidatesDomainName:NO];
    securityPolicy.allowInvalidCertificates = YES; //还是必须设成YES
    config.securityPolicy = securityPolicy;
}

@end
